﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PACS.Data;
using WoSoft.PACS.Data.Models;
using System.Data;

namespace WoSoft.PACS.Data.User
{
    /// <summary>
    /// 帐户管理
    /// </summary>
    public class AccountData : Db
    {
        /// <summary>
        /// 插入注册用户,成功返回true
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Insert(UserAccountModel model)
        {
            var cnt = this.SqlDatabase.Insert("系统用户")
                 .Column("ID", model.ID)
                 .Column("用户帐户", model.Account)
                 .Column("用户名称", model.Name)
                 .Column("用户密码", model.Pwd)
                 .Column("用户科室", model.DepartmentID)
                 .Column("医护人员ID", model.DoctorID)
                 .Column("用户角色", model.Role)
                 .Execute();

            return cnt > 0;
        }

        //更新用户
        public bool Update(UserAccountModel model)
        {
            return this.SqlDatabase.Update("系统用户")
                 .Column("用户帐户", model.Account)
                 .Column("用户名称", model.Name)
                 .Column("用户密码", model.Pwd)
                 .Column("用户科室", model.DepartmentID)
                 .Column("医护人员ID", model.DoctorID)
                 .Column("用户角色", model.Role)
                 .Where("ID", model.ID)
                 .Execute() > 0;
        }

        //删除
        public bool Delete(string id)
        {
            return this.SqlDatabase.Delete("系统用户")
                .Where("ID", id)
                .Execute() > 0;
        }

        /// <summary>
        /// 检查帐户是否存在?
        /// </summary>
        /// <param name="account">帐户名称</param>
        /// <returns></returns>
        public bool CheckAccountExists(string account, string id)
        {
            var sql = "select count(1) as 数量 from 系统用户 where (用户帐户<>@用户帐户 and ID=@ID and @ID is not null) or (用户帐户=@用户帐户 and @ID is null) ";
            var cnt = this.SqlDatabase.Sql(sql, null)
                 .Parameter("用户帐户", account)
                 .Parameter("ID", id)
                 .QuerySingle<int>();
            return cnt > 0;
        }

        /// <summary>
        /// 获取系统用户列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetAll()
        {
            var sql = "SELECT u.ID , u.用户名称 , u.用户帐户 ,  u.用户角色 , d.简称 AS 所属科室, h.姓名 AS 对应人员 FROM 系统用户 u , dbo.部门 d , dbo.医护人员 h WHERE u.医护人员ID = h.ID AND d.ID = u.用户科室";
            var dt = this.SqlDatabase.Sql(sql, null).QuerySingle<DataTable>();
            return dt;
        }

        //获取帐户信息
        public UserAccountModel GetModelByID(string accountid)
        {
            var sql = "select * from 系统用户 where ID = @id";
            var dyobj = this.SqlDatabase.Sql(sql, null)
                .Parameter("id", accountid)
                .QuerySingle<dynamic>();
            if (dyobj != null)
            {
                return new UserAccountModel()
                {
                    ID = dyobj.ID,
                    Account = dyobj.用户帐户,
                    DepartmentID = dyobj.用户科室 != null ? dyobj.用户科室 : string.Empty,
                    Name = dyobj.用户名称,
                    DoctorID = dyobj.医护人员ID != null ? dyobj.医护人员ID : string.Empty,
                    Role = dyobj.用户角色,
                    Pwd = dyobj.用户密码
                };
            }

            return null;
        }

        //登录
        public UserAccountModel Login(string userAccount, string password)
        {
            var sql = "select t.*,d.简称 as 部门名称 from 系统用户 t, dbo.部门 d where t.用户科室 = d.ID AND t.用户帐户 = @用户帐户 and t.用户密码=@用户密码";
            var dyobj = this.SqlDatabase.Sql(sql, null)
                .Parameter("用户帐户", userAccount)
                .Parameter("用户密码", password)
                .QuerySingle<dynamic>();
            if (dyobj != null)
            {
                return new UserAccountModel()
                {
                    ID = dyobj.ID,
                    Account = dyobj.用户帐户,
                    DepartmentID = dyobj.用户科室 != null ? dyobj.用户科室 : string.Empty,
                    Name = dyobj.用户名称,
                    DoctorID = dyobj.医护人员ID != null ? dyobj.医护人员ID : string.Empty,
                    DepartmentName = dyobj.部门名称,
                    Role = dyobj.用户角色,
                    Pwd = dyobj.用户密码
                };
            }

            return null;
        }
        /// <summary>
        /// 获取系统参数
        /// </summary>
        /// <param name="csmc">参数名称</param>
        /// <returns></returns>
        public string GetXTCS(string csmc)
        {
            var sql = "select * from [系统参数] where 参数名称=@sqlType";
            var text = string.Empty;
            var dt = this.SqlDatabase.Sql(sql)
                .Parameter("sqlType", csmc)
                .QuerySingle<DataTable>();
            if (dt.Rows.Count > 0)
            {
                text = dt.Rows[0]["参数值"].ToString();
            }

            return text;
        }
        /// <summary>
        /// 获取服务器日期
        /// </summary>
        /// <returns></returns>
        public string GetServerDate()
        {
            var sql = "select getdate()";
            var text = string.Empty;
            var dt = this.SqlDatabase.Sql(sql)
                .QuerySingle<DataTable>();
            if (dt.Rows.Count > 0)
            {
                text = Convert.ToDateTime(dt.Rows[0][0]).ToString("yyyy-MM-dd");
            }

            return text;
        }
    }
}
